@using DNA.Mvc.Community.Controllers
@using DNA.Mvc.Security;
@using DNA.Mvc.Text;
@model IEnumerable<Post>
@section HeaderSection{
    <link type="text/css" rel="stylesheet" href="@Url.Content(WebSiteContext.Current.GetThemePath("community/style.css"))" />
}
@{
    //var threadPost = (Post)ViewData["ThreadPost"];
    ThreadWrapper thread = ViewBag.Thread;
    ForumWrapper forum = ViewBag.Forum;
    var context = WebSiteContext.Current;
    var isThreadModerator = thread.IsModerator(Context);
}
<div style="margin-bottom: 5px; height: 30px;">
    @Ajax.ForumMapPath(thread.Forum.ID)
</div>
<div style="clear: left;">
</div>
<div style="padding: 5px;">
    @{Html.RenderPartial("~/Areas/Community/Views/Forum/Detail.cshtml", forum); }
</div>
<br />
@if (Request.IsAuthenticated)
{ 
    <div style="display: inline-block; margin-bottom: 10px;">
        @if (context.IsAuthorized<PostController>("New"))
        {
            if ((forum.IsModerator(Context)) || (!forum.IsLocked))
            {
                Ajax.Dna().Button("NewThreadButton")
                    .HtmlAttributes(new { style = "float:left;" })
                     .ChangeType(ButtonTypes.LinkButton)
                    .NavigateUrl(Url.Action("New", "Post", new { Area = "Community", forumID = forum.ID }))
                    .Images(Url.Content("~/Content/Themes/" + context.Web.Theme + "/images/ui-forum-newthread.png"),
                    Url.Content("~/Content/Themes/" + context.Web.Theme + "/images/ui-forum-newthread-hover.png"))
                    .Render();
            }
        }
        @if (thread.IsApproved)
        {
            if (!thread.IsLocked)
            {
                if (context.IsAuthorized<PostController>("Lock") || isThreadModerator)
                {
                    Ajax.Dna().Button("LockButton")
                        .HtmlAttributes(new { style = "float:left;padding-top: 15px;" })
                        .ChangeType(ButtonTypes.LinkButton)
                        .ToolTip(Html.Global("forums", "Lock"))
                        .NavigateUrl(Url.Action("Lock", "Post", new { id = thread.ID }))
                        .Images(Url.ThemeContent("images/ui-forum-lock.png"), Url.ThemeContent("images/ui-forum-lock-hover.png"))
                        .Render();
                }
            }
            else
            {
                if (context.IsAuthorized<PostController>("Unlock") || isThreadModerator)
                {
                    Ajax.Dna().Button("btnUnlock")
                        .HtmlAttributes(new { style = "float:left;padding-top: 15px;" })
                        .ChangeType(ButtonTypes.LinkButton)
                        .ToolTip(Html.Global("forums", "Unlock"))
                        .NavigateUrl(Url.Action("Unlock", "Post", new { id = thread.ID }))
                        .Images(Url.ThemeContent("images/ui-forum-unlock.png"), Url.ThemeContent("images/ui-forum-unlock-hover.png"))
                        .Render();
                }
            }
            if (!thread.IsPinned)
            {
                if (context.IsAuthorized<PostController>("Pin") || isThreadModerator)
                {
                    Ajax.Dna().Button("btnPin")
                            .HtmlAttributes(new { style = "float:left;padding-top: 15px;" })
                            .ChangeType(ButtonTypes.LinkButton)
                            .ToolTip(Html.Global("forums", "Pin"))
                            .NavigateUrl(Url.Action("Pin", "Post", new { id = thread.ID }))
                            .Images(Url.ThemeContent("images/ui-forum-Pin.png"), Url.ThemeContent("images/ui-forum-pin-hover.png"))
                            .Render();
                }
            }
            else
            {
                if (context.IsAuthorized<PostController>("Unpin") || isThreadModerator)
                {
                    Ajax.Dna().Button("btnUnpin")
                          .HtmlAttributes(new { style = "float:left;padding-top: 15px;" })
                          .ChangeType(ButtonTypes.LinkButton)
                          .ToolTip(Html.Global("forums", "Unpin"))
                          .NavigateUrl(Url.Action("Unpin", "Post", new { id = thread.ID }))
                          .Images(Url.ThemeContent("images/ui-forum-Unpin.png"), Url.ThemeContent("images/ui-forum-Unpin-hover.png"))
                          .Render();
                }
            }

            if (isThreadModerator)
            {
                Ajax.Dna().Button("btnMove")
                        .HtmlAttributes(new { style = "float:left;padding-top: 15px;" })
                        .ChangeType(ButtonTypes.LinkButton)
                        .ToolTip(Html.Global("forums", "Move"))
                        .NavigateUrl(Url.Action("Move", "Post", new { id = thread.ID }))
                        .Images(Url.ThemeContent("images/ui-forum-Move.png"), Url.ThemeContent("images/ui-forum-Move-hover.png"))
                        .Render();
            }
        }
    </div>
}
<h1 id="topPost" class="d-forums-post-title">
    @thread.Title
    @if (!thread.IsApproved)
    {
        <span>[@Html.Global("forums", "PendingForReview")]</span>
    }
</h1>
<hr />
<table style="width: 100%;">
    @if (thread.IsApproved)
    {
        <tr>
            <td colspan="2">
                <table style="width: 100%;">
                    <tr>
                        <td style="width: 100px;">@Twitter.TweetButton("Horizontal")
                        </td>
                        <td style="width: 100px;">
                            <script src="http://platform.linkedin.com/in.js" type="text/javascript"></script>
                            <script type="IN/Share" data-counter="right"></script>
                        </td>
                        <td>@Facebook.LikeButton(action: "recommend", buttonLayout: "button_count", width: 150, height: 20)
                        </td>
                        <td style="text-align: right;">
                            @if (!string.IsNullOrEmpty(thread.Title))
                            {
                            
                                <span>@DNA.Mvc.UI.GE.Global("publishing", "ShareTo"):</span>@DNA.Mvc.UI.ShareLinks.GetHtml(thread.Title)
                            }
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td style="width: 100px;">
                <h2>@thread.TotalPosts
                    @Html.Global("forums", "Replies")</h2>
            </td>
            @if (Request.IsAuthenticated)
            {
                <td>@UIHelper.SubscriptionLink(Ajax, thread.SubscriptionToken)
                </td>
            }
        </tr>
    }
    <tr>
        <td colspan="2">
            @if (thread.LastPost != null)
            {
                <table>
                    <tr>
                        <td>
                            @Html.Global("forums", "LastPost"):
                        </td>
                        <td>
                            @Html.Global("forums", "By")
                        </td>
                        <td>@Html.UserLink(thread.Author, new { style = "float:left;" })
                        </td>
                        <td>
                            <a style="display: inline-block; clear: left; margin-left: 3px;" href="#@thread.LastPost.Pos">
                                <span class="forum-icon forum-icon-latest-topic" style="margin-top: 5px;"></span>
                            </a>
                        </td>
                        <td>
                            <div>
                                @switch ((DateTime.Today - thread.LastPost.Posted).Days)
                                {
                                    case 0:
                                    <b>@Html.Global("forums", "Today")</b> @Html.Global(thread.LastPost.Posted, true)
                                                                      break;
                                    case 1:
                                    <b>@Html.Global("forums", "Yesterday")</b> @Html.Global(thread.LastPost.Posted, true) 
                                                                          break;
                                    default:
                                    @Html.Global(thread.LastPost.Posted)
                               break;
                                }
                            </div>
                        </td>
                    </tr>
                </table>
            }
        </td>
    </tr>
</table>
<table style="width: 100%">
    <tr>
        <td style="width: 50%;">
            @if (ViewBag.PrevThread != null)
            { 
                @Ajax.Dna().Link(Url.Action("Index", "Thread", new { Area = "Community", id = ViewBag.PrevThread.ID })).ImageIcons(Url.Content("~/content/images/icon_back_16.png")).Text("Previous thread:[" + Html.Cut((string)ViewBag.PrevThread.Title, 50) + "]").GetHtml()
            }
        </td>
        <td style="width: 50%; text-align: right">
            @if (ViewBag.NextThread != null)
            {
                @Ajax.Dna().Link(Url.Action("Index", "Thread", new { Area = "Community", id = ViewBag.NextThread.ID })).ImageIcons("", Url.Content("~/content/images/icon_next_16.png")).Text("Next thread:[" + Html.Cut((string)ViewBag.NextThread.Title, 50) + "]").GetHtml()
            }
        </td>
    </tr>
</table>
<ul class="d-forums-post-list">
    @foreach (var p in Model)
    {
        var postClass = "d-forums-post";
        var post = new PostWrapper(p);
        var isPostOwner = post.IsOwner(Context);
        var isPostModerator = post.IsModerator(Context);

        if ((post.IsApproved) || isPostModerator || isPostOwner)
        {
            postClass = (post.Pos % 2 == 0) ? "d-forums-post" : "d-forums-odd-post";
        }
        <li class="@postClass">
            <table style="width: 100%;">
                <tr>
                    <td>
                        <h2 class="d-forums-post-title">
                            <strong id="@post.ID">#@post.Pos</strong>&nbsp;&nbsp;@Html.Global(post.Posted)
                        </h2>
                    </td>
                    <td>
                        <ul class="d-forums-post-button-list">
                            @if (!post.IsDeleted)
                            {
                                if (!post.IsApproved)
                                {
                                    if (isPostModerator || (context.IsAuthorized<PostController>("Audit")))
                                    {
                                <li>
                                    @(Ajax.Dna().Button("btnAudit_" + post.Pos)
                                    .ToolTip(Html.Global("forums", "Audit"))
                                    .ChangeType(ButtonTypes.LinkButton)
                                            .NavigateUrl(Url.Action("Audit", "Post", new { id = post.ID, Area = "Community" }))
                                    .Images(Url.ThemeContent("images/ui-forum-audit.png"), Url.ThemeContent("images/ui-forum-audit-hover.png"))
                                    .GetHtml())
                                </li>
                                    }
                                }

                                if (context.IsAuthorized<PostController>("Edit"))
                                {
                                    if (isPostOwner || isPostModerator)
                                    { 
                                <li>
                                    @(Ajax.Dna().Button("btnEdit_" + post.Pos)
                                    .ToolTip(Html.Global("forums", "Edit"))
                                    .ChangeType(ButtonTypes.LinkButton)
                                    .NavigateUrl(Url.Action("Edit", "Post", new { id = post.ID }))
                                    .Images(Url.ThemeContent("images/ui-forum-edit.png"), Url.ThemeContent("images/ui-forum-edit-hover.png"))
                                    .GetHtml())
                                </li>
                                    }
                                }

                                if (context.IsAuthorized<PostController>("Delete"))
                                {
                                    if (isPostOwner || isPostModerator)
                                    {
                                <li>
                                    @(Ajax.Dna().Button("btnDelete_" + post.Pos)
                            .ToolTip(Html.Global("forums", "Delete"))
                            .ChangeType(ButtonTypes.LinkButton)
                            .NavigateUrl(Url.Action("Delete", "Post", new { id = post.ID }))
                            .Images(Url.ThemeContent("images/ui-forum-delete.png"), Url.ThemeContent("images/ui-forum-delete-hover.png"))
                            .GetHtml())
                                </li>
                                    }
                                }

                                if ((!thread.IsLocked) && (thread.IsApproved))
                                {
                                    if (isPostOwner || isPostModerator || (context.IsAuthorized<PostController>("Reply")))
                                    {
                                <li>
                                    @(Ajax.Dna().Button("btnReply_" + post.Pos)
                                    .ToolTip(Html.Global("forums", "Reply"))
                                    .ChangeType(ButtonTypes.LinkButton)
                                    .NavigateUrl(Url.Action("Reply", "Post", new { id = thread.ThreadPost.ID, parentID = post.ID }))
                                    .Images(Url.ThemeContent("images/ui-forum-reply.png"), Url.ThemeContent("images/ui-forum-reply-hover.png"))
                                    .GetHtml())</li>
                                    }
                                }
                            }
                        </ul>
                    </td>
                </tr>
            </table>
            <hr />
            <div class="d-forums-post-body">
                <table style="width: 100%">
                    <tr>
                        <td class="d-forums-post-profile" style="vertical-align: top;">
                            @if (post.Profile != null)
                            {
                                Html.RenderPartial("UserInfo", post.Profile);
                            }
                        </td>
                        <td class="d-forums-post-content">
                            <div style="padding: 5px; margin-top: 5px; min-height: 200px;">
                                @if ((!post.IsApproved) && (isPostOwner))
                                {
                                    <div class="ui-state-highlight" style="padding: 5px; background: none repeat scroll 0 0 #FFFFFF;
                                        margin: 5px;">
                                        <img src ="@Url.Content("~/content/images/info.gif")" style="vertical-align:middle;margin-right:5px;" alt="wait"/>@Html.Global("forums", "PostWaitForAudit")
                                    </div>
                                }
                                @if (!post.IsDeleted)
                                {
                                    @TextEngine.BBCode(post.Body, false, false, true)
                                }
                                else
                                { 
                                    <div class="ui-state-error" style="margin: 5px; padding: 5px;">
                                        <div>@string.Format(Html.Global("forums", "PostDeleteBy"), post.DeleteBy)</div>
                                        <div>@Html.Global("forums", "PostDeleted"):@Html.Global(post.Deleted)</div>
                                        <div>@Html.Global("forums", "Reason"): @post.DeletedReason</div>
                                    </div>
                                }
                            </div>
                            @if (post.HasAttachments)
                            {
                                if (HttpContext.Current.Request.IsAuthenticated)
                                {
                                <div style="margin-top: 10px; margin-bottom: 10px;">
                                    <div>
                                        <b>@Html.Global("forums", "Attachments"):</b></div>
                                    @{ Html.RenderPartial("Attachments", post.Attachments); }
                                </div>
                                }
                                else
                                {
                                <div class="ui-state-highlight">
                                    @Html.Global("forums", "NotSeeBeforeLogin") - @Html.ActionLink("login", "Login", "Account", new { Area = "" }, null)
                                </div>
                                }
                            }
                            @if (!string.IsNullOrEmpty(post.Profile.Signature))
                            { 
                                <div style="padding: 5px 0px;">
                                    <hr />
                                    @MvcHtmlString.Create(post.Profile.Signature)
                                </div>
                            }
                        </td>
                    </tr>
                </table>
                <div style="float: left; height: 20px; width: 100%;">
                    @Html.ImageLinkList().Options(styles =>
               {
                   styles.CssClass = "d-forums-post-button-list";
                   styles.ItemCssStyleText = "margin-right:5px;";
                   styles.ImageCssStyleText = "float:left;margin-right:5px;";
               }).Items(items =>
               {
                   items.Add(Html.Global("RES_IP") + ":" + ((Request.IsAuthenticated) && (User.IsInRole("administrators")) ? post.IP : "******"), Url.Content("~/content/images/cloud.gif"));
                   items.Add("", Url.ThemeContent("images/ui-gotop.png"), "#topPost");
               }).Render()
                </div>
            </div>
        </li>
    }
</ul>
@if (!Request.Browser.Crawler)
{
    @Ajax.Dna().Pager("threadPager").Load(Url.Action("index")).GetHtml()
}
